<template>
	<view>
		<view class="content">
			<!-- 待用活动表单1 -->
			<active-form :formDate="formDate1"  @sure-btn="sure1" ref="child" :fid="fid" @input-val="inputVal1">
			</active-form>
		</view>
	</view>
</template>

<script>
	import activeForm from './../../components/active_form.vue';
	export default {
		components: {
			activeForm
		},
		data() {
			return {
				formDate1: [],
				fid: 0
			}
		},
		onShow() {
			this.select();
			
		},
		onLoad(e) {
			uni.setNavigationBarColor({
				frontColor: "#ffffff",	//文字颜色
				backgroundColor: this.$kdup.color	//底部背景色
			})
			var params = {
				billtypeId: e.id,
				userid: "155347"
			}
			this.$myRequest.postRequest("AddConfigTable", params).then(res1 => {
				uni.setNavigationBarTitle({
					title: e.name,
				});
				//获取临时表id
				var obj = res1.data;
				this.fid = res1.data.fid;
				this.$kdup.fid = this.fid;
				if (!this.$kdup.isEmpty(res1.data.fheadinput)) {
					var text = JSON.parse(res1.data.fheadinput);
				}
				var data = {
					billTableId: e.id
				}
				this.$myRequest.postRequest("GetConfigTable", data).then(res => {
					if (res.data.error == "") {
						//初始化请求查询当前用户在临时表有没有e.id这个单据  userid模拟数据，以后改成登录用户的id
						//找到数据		
						for (var i = 0; i < res.data.Table.length; i++) {
							if (i == 0) {
								this.$kdup.config = res.data.Table[i];
							}
							var row_value = {
									id: ""
								},
								dispalyValue = "";
							if (!this.$kdup.isEmpty(text)) {
								var str1 = this.$kdup.getJsonVal(text, res.data.Table[i].ffieldname, "");
								if (!this.$kdup.isEmpty(str1))
									row_value = JSON.parse(str1);
								dispalyValue = row_value.hasOwnProperty("name") ? row_value["name"] : row_value["id"];
							}

							if (res.data.Table[i].fscanposition != 1) {
								//表头ffieldposition:1/表体ffieldposition:0
								this.formDate1.push({
									placeholder: "请输入" + res.data.Table[i].ftitle, //提示
									label: res.data.Table[i].ftitle, //标题
									type: this.$kdup.istype(res.data.Table[i].ffieldtype), //类型ffieldtype
									disabled: false, //是否启用
									isshowdel: res.data.Table[i].fisshowdel == 0 ? false : true, //是否显示删除图标
									isshowscan: res.data.Table[i].fiscamera == 0 ? false : true, //是否显示扫描图标
									maxlength: "-1", //输入长度,
									isEnterCallBack: res.data.Table[i].ffieldtype == 6 || res.data.Table[i].ffieldtype == 4? true : false,
									firstFocus: false,
									key: res.data.Table[i].fkey, //唯一标识
									property: res.data.Table[i], //配置属性
									rules: {
										flag: res.data.Table[i].ffieldtype, //标识
										name: res.data.Table[i].fkey, //名称fkey
										value: dispalyValue, //显示值
										rowvalue: row_value, //保存值
										verify: res.data.Table[i].fmustinput == 0 ? "" : "req", //必录fmustinput
										errMess: "请输入" + res.data.Table[i].ftitle //非空提示，
									}
								})
							}
						}
				        this.select();
					} else {

						//未找到数据
						uni.showToast({
							title: res.data.error,
							icon: "none",
							duration: 3000
						});
					}
				})
			})
		},
		methods: {
			//初始化选中在onload里面
			//页面显示选中在onshow里面
			//选值后选中调用上一个页面的select
			//获取焦点后禁用键盘
			//失去后取消全部焦点
			//找不到值点击确定或者取消后调用select
			//扫描后或者用pad扫描后调用上一个页面select
			select :function(){
				for (var i = 0; i < this.formDate1.length; i++) {
					if(this.formDate1[i].hasOwnProperty("firstFocus") && this.$kdup.isEmpty(this.formDate1[i].rules.value)){
						this.formDate1[i].firstFocus = true;
						break;
					} 
				}
					uni.hideKeyboard();
			},
			//设置页签1表单的值
			inputVal1: function(data) {
				var data = JSON.parse(data);
				if (String(data.val) != "" && String(data.index) != "") {
					this.formDate1[data.index].rules.value = data.val;
					if (String(data.rowvalue) != "" || String(data.rowvalue) != null) {
						this.formDate1[data.index].rules.rowvalue = data.rowvalue;
					}
				}
			},
			sure1: function(data) {
				var data = JSON.parse(data);
				if (String(data.currentSelectIndex) != "" && String(data.currentSelectValue) != "") {
					this.formDate1[data.currentSelectIndex].rules.value = data.currentSelectValue.name;
					if (String(data.rowvalue) != "" || String(data.rowvalue) != null) {
						this.formDate1[data.currentSelectIndex].rules.rowvalue = data.currentSelectValue;
					}
				}
			},
			submitAct: function(index, row) {
				this.$refs.child.enter(index, this.formDate1[index].property, row.number != undefined ? row.number : row.fbarcode);
			},
			TypeControl(key){
				var data={type:"",flag:false};
				if((key=="批号"||key=="生产日期"||key=="扫描状态") && this.fid==81){
				 	data.flag=false;
				}else{
					data.flag=true;
				}
				if(key=="生产日期" && this.fid==81){
					data.type="date"; 
				}else if(key=="扫描状态"){
					data.type="radio"; 
				}else{
					data.type="text"; 
				}
				return data;
			},
			enterCallbackFunc: function(index, row) {
		        if(this.fid==81 && this.formDate1.length==15){
					this.formDate1.splice(2);
				}else if(this.fid==82){
						this.formDate1.splice(2);
				}
				for (var i = this.formDate1.length - 1; i >= 0; i--) {
					if (this.formDate1[i].hasOwnProperty("isdel") && this.formDate1[i].isdel) {
						this.formDate1.splice(i, 1)
					}
				}
				for(var i = 0; i< row.Table.length-1; i++){
					for (var key in row.Table[i]) {
						this.formDate1.push({
							label: key, //标题
							type:this.TypeControl(key).type, //类型ffieldtype
							disabled:this.TypeControl(key).flag, //是否启用
							isshowdel:(key=="生产日期")?true:false,
							isdel:(key=="生产日期")?true:false,
							showLine:false,
							property: {
								fishidden: 0,
							},
							rules: {
								flag: 0, //标识
								name: key+i, //名称fkey
								value: row.Table[i][key], //显示值
								rowvalue: {number:row.Table[i][key],id:row.Table[i][key],name:row.Table[i][key]}, //保存值
								verify: "", //必录fmustinput
								
							}
						})
					}
					this.formDate1[this.formDate1.length-1].showLine = true;
				}
				this.select();
				
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>
